﻿colnames(hallmark_score) <- str_remove_all(colnames(hallmark_score),'HALLMARK_') %>% str_replace_all('_','')
cor_res <- psych::corr.test(hallmark_score,hallmark_score,method = 'spearman',adjust = 'BH')

pdf(paste0(out_home,'/out/2.hallmark_score/Fig_hallmark_rho.pdf'), width = 7, height = 7)
p <- corrplot::corrplot(cor_res$r,
    method = "circle",
    type = "upper",
    order = "original",
    # addCoef.col = "#1C1C1C",
    tl.col = "black",
    tl.cex = 0.5,
    col = colorRampPalette(c("#297eb6", "#FFFFFF","#F36C43"))(10),
    p.mat= cor_res$p.adj,
    insig = "label_sig",
    sig.level = c(0.001,0.01,0.05),
    pch.cex = .3,pch.col="black",
     is.corr = F # ,
    # cl.lim = c(0,1)
)
dev.off()


res <- psych::corr.test(m1, m2)
library(corrplot)
library(RColorBrewer)
pdf(file = "results/yk_vis/3.xgene_in_dat_and_validat/3.关键marker表达之间的相关性分析_heat.pdf", width = 4, height = 4)
corrplot(as.matrix(res$r),
    method = "color", # c("circle", "square", "ellipse", "number", "shade", "color", "pie"),
    tl.col = "black",
    type = "upper",
    is.corr = T,
    tl.srt = 45, tl.cex = .75, # 字体大小和角度
    p.mat = res$p,
    insig = "label_sig", # "pch", "p-value", "blank", "n", "label_sig"
    sig.level = c(.0001, .001, 0.01, 0.05),
    pch.cex = .5,
    cl.pos = "b",
    cl.length = 5,
    cl.ratio = .14,
    col = colorRampPalette(c("blue2", "white", "red2"))(41),
    outline = T
)
dev.off()



dat$data_log2_fpkm[xgene,] %>% t() %>% as.data.frame() -> m2 -> m1
res <- psych::corr.test(m1,m2)
library(corrplot)
library(RColorBrewer)
pdf(file = "out/6.六、SA-AKI关键marker的表达与相关性分析/关键marker表达之间的相关性分析.pdf", width = 4, height = 4)
corrplot(as.matrix(res$r),
    method = "color", # c("circle", "square", "ellipse", "number", "shade", "color", "pie"),
    tl.col = "black",
    type = 'upper',
    is.corr = T,
    tl.srt = 45, tl.cex = .75, # 字体大小和角度
    p.mat = res$p,
    insig = "label_sig", # "pch", "p-value", "blank", "n", "label_sig"
    sig.level = c(.0001,.001,0.01, 0.05),
    pch.cex = .5,
    cl.pos = 'b',
    cl.length = 5,
    cl.ratio = .08,
    col = colorRampPalette(c("blue2","white", "red2"))(41),
    outline = T
)
dev.off()


library(ggstatsplot)
library(rlang)
pl2 <- map(colnames(m1), \(.x) {
    # .x <- colnames(m1)[1]
    pl <- map(setdiff(colnames(m1), .x), \(.y) {
        # .y <- setdiff(colnames(m1), .x)[1]
        res <- cor.test(m1[[.x]], m1[[.y]])
        p <- res$p.value
        r <- res[["estimate"]] %>% round(2)
        str_ <- str_glue("cor: {r}; p{format.pval(p)}")

        p <- ggscatterstats(
            data = m1,
            x = {{ .x }},
            y = {{ .y }},
            xlab = .x,
            ylab = .y,
            results.subtitle = F,
            subtitle = str_,
            point.args = list(size = 1, alpha = 0.72, stroke = 0),
            title = "Gene expression correlation"
        )
        p
    })
    p2 <- ggarrange(plotlist = pl, nrow = 3, ncol = 3)
    return(p2)
})
names(pl2) = colnames(m1)

walk(names(pl2), ~ ggsave3(
    plot = pl2[[.x]],
    filename = str_glue("out/6.六、SA-AKI关键marker的表达与相关性分析/2.关键marker表达之间的相关性scatter_plot/{.x}.pdf"),
    width = 9, height = 9
))

